package handler

import (
	"server/form"
	"server/initialize"
	"server/model"
	"strconv"

	"github.com/gin-gonic/gin"
)

func Ping(c *gin.Context) {
	initialize.MapChannel <- new(interface{})
	c.JSON(200, gin.H{
		"code":    0,
		"success": true,
		"msg":     "pong",
	})
}

func GetAllJson(c *gin.Context) {
	json, err := GetJson()
	if err != nil {
		c.JSON(200, gin.H{
			"code":  -1,
			"error": err.Error(),
			"msg":   "获取失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 0,
		"data": json,
		"msg":  "获取成功",
	})
}

func GetAllData(c *gin.Context) {
	c.JSON(200, gin.H{
		"code": 0,
		"data": model.GetDataList(),
		"msg":  "获取成功",
	})
	return
}

func Login(c *gin.Context) {
	req := c.DefaultQuery("shengxiao", "0")
	defer func() {
		recover()
	}()
	if req != "0" {
		// 校验生肖是否合理
		atoi, err := strconv.Atoi(req)

		if err != nil || atoi <= 0 || atoi > 12 {
			c.JSON(200, gin.H{
				"code":  -1,
				"error": "未指定生肖",
				"msg":   "请求参数不正确",
			})
			return
		}

		// 分配uuid之前  删除之前存在的uuid
		useUUID, _ := c.Cookie("uuid")
		if useUUID != "" {
			//	本身存在uuid
			model.DeleteDataByUUid(useUUID)
		}

		//	分配UUid
		uuid := initialize.GenID()
		model.SetDataByUUid(model.GameData{
			Uuid:      uuid,
			Shengxiao: atoi,
			X:         0,
			Y:         0,
			Score:     0,
		})
		c.SetCookie("uuid", uuid, 3600, "", "", false, false)
		// 用协程 复制数据到redis
		//go SetJson(model.GetDataList())
		initialize.MapChannel <- new(interface{})
		c.JSON(200, gin.H{
			"code": 0,
			"data": gin.H{
				"uuid": uuid,
			},
			"msg": "注册成功",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code":  -1,
			"error": "未指定生肖",
			"msg":   "请求参数不正确",
		})
		return
	}
}

func Logout(c *gin.Context) {
	// 分配之前删除 之前存在的uuid
	useUUID, _ := c.Cookie("uuid")
	if useUUID != "" {
		//	本身存在uuid
		model.DeleteDataByUUid(useUUID)
		c.SetCookie("uuid", "", -1, "", "", false, false)
	}
	useUUID = c.DefaultQuery("uuid", "")
	if useUUID != "" {
		//	本身存在uuid
		model.DeleteDataByUUid(useUUID)
		c.SetCookie("uuid", "", -1, "", "", false, false)
	}

	//go SetJson(model.GetDataList())

	initialize.MapChannel <- new(interface{})
	c.JSON(200, gin.H{
		"code": 0,
		"msg":  "退出成功",
	})
	return
}

// Move 传入无效的uuid 无法完成新增且移动
func Move(c *gin.Context) {
	var req form.ReqGameData
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(200, gin.H{
			"error": initialize.Translate(err),
			"msg":   "fail",
		})
		return
	}

	if req.Uuid != "" {
		data, err := model.GetDataByUUid(req.Uuid)
		if err != nil {
			c.JSON(200, gin.H{
				"msg": "not found uuid",
			})
			return
		}
		data.X = req.X
		data.Y = req.Y
		model.ChangeDataByUUid(*data)
		//go SetJson(model.GetDataList())
		c.JSON(200, gin.H{
			"msg":  "success",
			"data": data,
		})
		return
	} else {
		c.JSON(200, gin.H{
			"msg": "not found uuid",
		})
		return
	}

}

func MoveX(c *gin.Context) {
	var req form.ReqMoveX
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(200, gin.H{
			"error": initialize.Translate(err),
			"msg":   "fail",
		})
		return
	}

	if req.Uuid != "" {
		data, err := model.GetDataByUUid(req.Uuid)
		if err != nil {
			c.JSON(200, gin.H{
				"msg": "not found uuid",
			})
			return
		}
		data.X = data.X + req.X
		model.ChangeDataByUUid(*data)
		//go SetJson(model.GetDataList())
		c.JSON(200, gin.H{
			"msg":  "success",
			"data": data,
		})
		return
	} else {
		c.JSON(200, gin.H{
			"msg": "not found uuid",
		})
		return
	}

}

func MoveY(c *gin.Context) {
	var req form.ReqMoveY
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(200, gin.H{
			"error": initialize.Translate(err),
			"msg":   "fail",
		})
		return
	}

	if req.Uuid != "" {
		data, err := model.GetDataByUUid(req.Uuid)
		if err != nil {
			c.JSON(200, gin.H{
				"msg": "not found uuid",
			})
			return
		}
		data.Y = data.Y + req.Y
		model.ChangeDataByUUid(*data)
		//go SetJson(model.GetDataList())
		c.JSON(200, gin.H{
			"msg":  "success",
			"data": data,
		})
		return
	} else {
		c.JSON(200, gin.H{
			"msg": "not found uuid",
		})
		return
	}

}

func ResetGame(c *gin.Context) {
	model.MapData = model.NewRWMap(0)

	c.JSON(200, gin.H{
		"code": 0,
		"msg":  "重置成功",
	})
	return
}
